package com.wl.jetpackdemo.base.utils

import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText




/**
 *
 * Created by wangliang on 2021/2/20.
 */
class KeyboardUtils {
    companion object {

        fun showOrHideInputKeyboard(context: Context) {
            val imm = context
                    .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
        }

        fun showInputKeyboard(et: EditText) {
            val imm = et.context
                    .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT)
        }

        /**
         * 强制隐藏键盘
         * @param context
         * @param et
         */
        fun forceHideInputKeyboard(context: Context, et: EditText) {
            et.clearFocus()
            hideInputKeyboard(context, et)
        }

        fun hideInputKeyboard(context: Context, et: EditText) {
            val imm: InputMethodManager = context
                    .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(et.windowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS)
        }

    }
}